<?php
require_once('kernel.inc');
$vars = false;

if($AUTH->logedstatus === true && $AUTH->permission == 'system')
{

   if(isset($_REQUEST['dele']))
   {
      $AUTH->DeleteUsers($_REQUEST['dele']);
      SmartyPaginate::reset($_SYS['meccano']['pid']);
      SmartyPaginate::setLimit($MECCANO->system['items'], $_SYS['meccano']['pid']);
      SmartyPaginate::setPageLimit($MECCANO->system['pages'], $_SYS['meccano']['pid']);
   }
   
   switch ($event)
   {
      # UPDATE USER INFO
      case 1:
         if($id)
         {
            $SMARTY->assign('user', $AUTH->GetUserInfo($id));
         }

      #ADD NEW USER
      case 2:
         if($submit)
         {
            $in['login']  = isset($_REQUEST['login'])  ? isAlpha($_REQUEST['login']) : false;
            $in['pwd_1']  = isset($_REQUEST['pwd_1'])  ? isAlpha($_REQUEST['pwd_1']) : false;
            $in['pwd_2']  = isset($_REQUEST['pwd_2'])  ? isAlpha($_REQUEST['pwd_2']) : false;
            $in['name']   = isset($_REQUEST['name'])   ? $_REQUEST['name']           : '';
            $in['family'] = isset($_REQUEST['family']) ? $_REQUEST['family']         : '';
            $in['email']  = isset($_REQUEST['email'])  ? isEmail($_REQUEST['email']) : '';

            if($id == false && $in['login'] == false) $error[] = 'login';
            if($in['pwd_1'] != $in['pwd_2'] || ($id == false && $in['pwd_1'] == false)) $error[] = 'pwd';

            if($id && !$error)
            {
               $in['pwd'] = $in['pwd_1'];
               $make = $AUTH->UpdateUserInfo($id, $in);
            } elseif(!$error)
            {
               $make = $AUTH->AddUser($in);
            } else
            {
               $SMARTY->assign('user', $in);
            }
         }

         $SMARTY->assign('error', $error);
         $SMARTY->assign('make', $make);
         $SMARTY->display('admin.edituser.tpl.html');
         break;

      # MANAGE RIGHTS
      case 35:
         if($id)
         {
            $user = $AUTH->GetUserInfo($id);
            $SMARTY->assign('user', $user);
            var_dump($user);
         }

         $SMARTY->display('admin.rights.tpl.html');
         break;

      # CHANGE USER STATUS
      case 3:
      case 4:
         if($id !== false)
         {
            if($event == 3) $AUTH->SetValid($id, true);
            if($event == 4) $AUTH->SetValid($id, false);
         }

      default:

         $DB->sql = "SELECT * FROM meccano_users ORDER BY id";
         $vars = $DB->DB_Fetch();

         if($vars)
         {
            SmartyPaginate::setTotal(count($vars), $_SYS['meccano']['pid']);
            SmartyPaginate::setUrl($_SERVER['PHP_SELF'], $_SYS['meccano']['pid']);
            $SMARTY->assign('vars', array_slice($vars, SmartyPaginate::getCurrentIndex($_SYS['meccano']['pid']), SmartyPaginate::getLimit($_SYS['meccano']['pid'])));
            SmartyPaginate::assign($SMARTY, 'paginate', $_SYS['meccano']['pid']);
         }

         $SMARTY->assign('vars', $vars);
         $SMARTY->display('admin.users.tpl.html');
   }

} else
{
   $AUTH->frame_redirector();
}
?>